import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;
import net.miginfocom.swing.*;
/*
 * Created by JFormDesigner on Mon Dec 23 14:55:10 CST 2024
 */



/**
 * @author jjchen
 */
public class MF_bookmanager_modifybookinformation extends JFrame {
    public MF_bookmanager_modifybookinformation() {
        initComponents();
    }

    private void button1MouseClicked(MouseEvent e) {
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        bookManagement mb = new bookManagement();
        mb.setVisible(true);
    }

    private void button1MouseClicked2(MouseEvent e) {
        String book_id = textField1.getText();
        String book_title = textField3.getText();
        String writer = textField4.getText();
        String number = textField5.getText();
        String position = textField6.getText();

        String url = "jdbc:mysql://localhost:3306/javaproject";
        String username = "root";
        String rootPassword = "root"; // 注意：不要在生产环境中使用硬编码的密码

        String sql = "UPDATE book_information SET book_title=?, writer=?, number=?, position=? WHERE book_id=? ;";

        try (Connection conn = DriverManager.getConnection(url, username, rootPassword);
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            // 假设user_ID是整数类型，如果不是，请根据实际类型调整
            stmt.setString(1, book_title);
            stmt.setString(2, writer);
            stmt.setString(3, number);
            stmt.setString(4, position);
            stmt.setString(5, book_id);

            int count = stmt.executeUpdate();
            System.out.println("Updated rows: " + count);

            JOptionPane.showMessageDialog(this, "修改图书信息成功！");
        } catch (NumberFormatException ex) {
            // 处理userId不是整数的情况
            System.err.println("Invalid user_ID: " + book_id);
        } catch (SQLException ex) {
            // 处理SQL异常
            ex.printStackTrace();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label8 = new JLabel();
        label1 = new JLabel();
        textField1 = new JTextField();
        label4 = new JLabel();
        label5 = new JLabel();
        textField3 = new JTextField();
        label6 = new JLabel();
        textField4 = new JTextField();
        label9 = new JLabel();
        label7 = new JLabel();
        textField5 = new JTextField();
        label10 = new JLabel();
        textField6 = new JTextField();
        button1 = new JButton();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(new MigLayout(
            "hidemode 3",
            // columns
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]",
            // rows
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]"));

        //---- label8 ----
        label8.setText("\u4fee\u6539\u56fe\u4e66\u4fe1\u606f");
        label8.setFont(label8.getFont().deriveFont(label8.getFont().getSize() + 5f));
        contentPane.add(label8, "cell 5 5 1 2");

        //---- label1 ----
        label1.setText("\u8bf7\u8f93\u5165\u60a8\u8981\u4fee\u6539\u7684\u56fe\u4e66id\uff1a");
        contentPane.add(label1, "cell 1 10");
        contentPane.add(textField1, "cell 5 10 12 1");

        //---- label4 ----
        label4.setText("\u4fee\u6539\u540e\u7684\u4fe1\u606f\uff1a");
        contentPane.add(label4, "cell 1 15");

        //---- label5 ----
        label5.setText("\u540d\u79f0\uff1a");
        contentPane.add(label5, "cell 4 15");
        contentPane.add(textField3, "cell 5 15 12 1");

        //---- label6 ----
        label6.setText("\u4f5c\u8005\uff1a");
        contentPane.add(label6, "cell 4 17");
        contentPane.add(textField4, "cell 5 17 12 1");

        //---- label9 ----
        label9.setText("\u6570\u91cf\uff1a");
        contentPane.add(label9, "cell 4 18 1 4");
        contentPane.add(label7, "cell 5 18 13 1");
        contentPane.add(textField5, "cell 5 19 12 1");

        //---- label10 ----
        label10.setText("\u4f4d\u7f6e\uff1a");
        contentPane.add(label10, "cell 4 22");
        contentPane.add(textField6, "cell 5 22 12 1");

        //---- button1 ----
        button1.setText("\u786e\u5b9a\u4fee\u6539");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
                button1MouseClicked2(e);
            }
        });
        contentPane.add(button1, "cell 5 28");
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    public static void main(String[] args) {
        MF_bookmanager_modifybookinformation mf = new MF_bookmanager_modifybookinformation();
        mf.setVisible(true);
        mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label8;
    private JLabel label1;
    private JTextField textField1;
    private JLabel label4;
    private JLabel label5;
    private JTextField textField3;
    private JLabel label6;
    private JTextField textField4;
    private JLabel label9;
    private JLabel label7;
    private JTextField textField5;
    private JLabel label10;
    private JTextField textField6;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
